import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Button,
  Select,
  Tooltip,
  DatePicker,
  Icon,
} from 'antd';
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import styles from '../UserManage.less';
import { powerBtnFunc } from '@/utils/utils';

const FormItem = Form.Item;

/* eslint react/no-multi-comp:0 */
@connect(({ rewardModels, loading }) => ({
  rewardModels,
  loading: loading.models.rewardModels,
}))

@Form.create()
class DeptManage extends PureComponent {
  state = {
    formValues: {},
    pagination: {
      pageNo: 1,
      pageSize: 15,
    },
  };


  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'rewardModels/fetchLog',
    });

  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;


    this.setState({
      pagination: {
        pageNo: pagination.current,
        pageSize: 15,
      },
    });
    const params = {
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
    };
    dispatch({
      type: 'rewardModels/fetchLog',
      payload: params,
    });
  };
  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'rewardModels/fetchLog',
      payload: {},
    });
  };

  //搜索
  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
      };
      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'rewardModels/fetchLog',
        payload: values,
      });
    });
  };


  renderSimpleForm() {
    const { form: { getFieldDecorator } } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="用户姓名">
              {getFieldDecorator('appUserName')(<Input placeholder="请输入用户姓名"/>)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="发放类型">
              {getFieldDecorator('type')(
                <Select placeholder={'请选择奖励类型'} style={{ width: '100%' }}>
                  <Select.Option value={0} key={0}>全部</Select.Option>
                  <Select.Option value={1} key={1}>红包</Select.Option>
                  <Select.Option value={2} key={2}>优惠券</Select.Option>
                </Select>,
              )}
            </FormItem>
          </Col>

          <Col md={8} sm={24}>
            <FormItem label="发放原因">
              {getFieldDecorator('remark')(<Input placeholder="请输入发放原因"/>)}
            </FormItem>
          </Col>
        </Row>
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="开始时间">
              {getFieldDecorator('startTime')(<DatePicker placeholder="选择时间"
                                                          style={{ width: '100%' }}/>)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="结束时间">
              {getFieldDecorator('endTime')(<DatePicker placeholder="选择时间"
                                                        style={{ width: '100%' }}/>)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <div style={{ textAlign: 'center' }}><Button type="primary" htmlType="submit">
              查询
            </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </div>
          </Col>
        </Row>

      </Form>
    );
  }


  render() {
    const { rewardModels: { logData }, loading } = this.props;
    console.log(logData);
    const columns = [
      {
        title: '记录id',
        dataIndex: 'id',
        key: 'id',
      },
      {
        title: '奖励名称',
        key: 'couponName',
        render: val => {
          return <span>{val.type === 1 ? '红包' + val.amount / 100 + '元' : val.couponName}</span>;
        },
      },
      {
        title: '奖励编号',
        dataIndex: 'couponNo',
        key: 'couponNo',
      },
      {
        title: '用户ID',
        dataIndex: 'userNo',
        key: 'userNo',
      },
      {
        title: '用户姓名',
        dataIndex: 'userName',
        key: 'userName',
      },

      {
        title: '状态',
        key: 'status',
        render: val => <span>{val.status === 1 ? '成功' : <Tooltip title={val.reason}><Icon
          style={{ color: 'rgba(0, 0, 0, 0.43)', marginLeft: 4 }}
          type="info-circle-o"
        />失败</Tooltip>}</span>,

      },
      {
        title: '发放时间',
        key: 'sendTime',
        dataIndex: 'sendTime',
      },

      {
        title: '发放原因',
        dataIndex: 'remark',
        key: 'remark',
      },
      {
        title: '操作人',
        key: 'createName',
        dataIndex: 'createName',
      },
    ];
    return (
      <PageHeaderWrapper title="奖励记录">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>

            <StandardTable
              selectedRows={[]}
              loading={loading}
              rowSelectHide={true}
              data={logData ? logData : []}
              rowKey={'userNo'}
              columns={columns}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>


      </PageHeaderWrapper>
    );
  }
}

export default DeptManage;
